package com.BaoHua.demo.interceptor;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.BaoHua.demo.bean.Company;

@Component
public class AuthInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		Company user = UserContext.getUser();
		if(user == null) {
			String msg = URLEncoder.encode("请先登录","UTF-8");
			String target = URLEncoder.encode(request.getRequestURL().toString(),"UTF-8");
			if("GET".equalsIgnoreCase(request.getMethod())) {
				response.sendRedirect("/?errorMsg="+msg+"&target"+target);
			}else {
				response.sendRedirect("/?errorMsg="+msg);
			}
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		// TODO Auto-generated method stub

	}

}
